package com.citycloud.ccuap.tc.oauth2.controller;


import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.security.RolesAllowed;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerEndpointsConfiguration;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.citycloud.ccuap.tc.common.result.ResultObject;
import com.citycloud.ccuap.tc.oauth2.dto.BulkEncryptionDto;

@Controller
@RequestMapping("/backdoor")
@RolesAllowed({"SUPER_USER"})
public class BackdoorController {

    @Autowired
    private AuthorizationServerEndpointsConfiguration endpoints;

    @Autowired
    private TokenStore tokenStore;

    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    /**检查一个token的信息*/
    @RequestMapping(
            value = {"/getToken"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public ResultObject<Object> getToken(String token) {
        ResourceServerTokenServices tokenServices = endpoints.getEndpointsConfigurer().getResourceServerTokenServices();

        Map<String, Object> result = new HashMap<>(4);
        result.put("authentication", tokenServices.loadAuthentication(token));
        result.put("accesstoken", tokenServices.readAccessToken(token));

        return ResultObject.getInstance().success("获取token信息成功", result);
    }

    /**获取一个用户下所有token的信息*/
    @RequestMapping(
            value = {"/getTokenByUserNo"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public ResultObject<Object> getTokenByUserNo(String userNo) {

        if (tokenStore instanceof JdbcTokenStore) {
            JdbcTokenStore jdbcTokenStore = (JdbcTokenStore) tokenStore;
            Collection<OAuth2AccessToken> oAuth2AccessTokenList = jdbcTokenStore.findTokensByUserName(userNo);
            Map<String, Object> result = new HashMap<>(4);
            result.put("tokens", oAuth2AccessTokenList);
            result.put("userNo", userNo);
            return ResultObject.getInstance().success("获取token信息成功", result);
        } else {
            return ResultObject.getInstance().error("当前配置不支持此接口");
        }
    }

    /**批量的把明文变成密文*/
    @RequestMapping(
            value = {"/bulkEncryption"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public ResultObject<List<BulkEncryptionDto>> getTokenByUserNo(@RequestBody List<BulkEncryptionDto> list) {

        for (BulkEncryptionDto dto : list) {
            if(dto.getRawString() != null && !dto.getRawString().isEmpty()){
                dto.setEncryptedString(passwordEncoder.encode(dto.getRawString()));
            }
        }
        return ResultObject.getInstance().success(list);

    }

}
